//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.wyh.service.serviceImpl.appservieripml;

import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.dto.gfive.LoginUser;

import com.wyh.domain.po.gfive.User;
import com.wyh.domain.po.gfour.Contacts;
import com.wyh.domain.vo.gruopfour.customerVO.CustomerArrearsSummary;
import com.wyh.domain.vo.gruopfour.customerVO.CustomerContractSummary;
import com.wyh.domain.vo.gruopfour.customerVO.CustomerSummary;
import com.wyh.mapper.appmapper.CustomerMapper4;
import com.wyh.service.appservice.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private final CustomerMapper4 customerMapper4;

    @Autowired
    public CustomerServiceImpl(CustomerMapper4 customerMapper4) {
        this.customerMapper4 = customerMapper4;
    }


//     获取SecurityContext中的Authentication对象
    private static User getAdminUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            // 获取用户信息
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            return loginUser.getUser();
        } else {
            throw new CustomException(ResultCodeEnum.USER_NOT_LOGIN);
        }
    }


    public List<List<CustomerSummary>> getCustomerSummaryByPhone() {
        // TODO 拿到电话号
//        List<String> ids = customerSummaryDTO.getIds();
        String telephone = getAdminUser().getTelephone();
        Contacts contacts = getContactsByPhone(telephone);
        List<String> ids =  Arrays.asList(contacts.getCustomerid().split(","));
        List<CustomerArrearsSummary> summaryList = this.customerMapper4.selectCustomerArrearsSummaryByPhone(ids);
        List<CustomerContractSummary> customerContractSummaryList = this.customerMapper4.selectCustomerContractsByPhone(ids);
        List<CustomerSummary> list = (List)summaryList.stream().flatMap((customerArrearsSummary) -> {
            return customerContractSummaryList.stream().filter((customerContractSummary) -> {
                return customerArrearsSummary.getName().equals(customerContractSummary.getName());
            }).map((customerContractSummary) -> {
                CustomerSummary customerSummary = new CustomerSummary();
                customerSummary.setName(customerArrearsSummary.getName());
                customerSummary.setHouseCount(customerContractSummary.getHouseCount());
                customerSummary.setParkingSpaceCount(customerContractSummary.getParkingSpaceCount());
                customerSummary.setCountNonzeroArrears(customerArrearsSummary.getCountNonzeroArrears());
                customerSummary.setTotalArrears(customerArrearsSummary.getTotalArrears());
                customerSummary.setCustomerid(customerContractSummary.getCustomerid());
                return customerSummary;
            });
        }).collect(Collectors.toList());
        return Collections.singletonList(list);
    }

    @Override
    public Contacts getContactsByPhone(String phone) {
        return customerMapper4.getContactsByPhone(phone);
    }
}
